var  btnNumber = [].slice.call(document.getElementsByClassName('btnNumber')),
    phone = document.getElementById('phone'),
    nextBtn = document.getElementsByClassName('nextBtn')[0],
    btn_num = document.getElementsByClassName('btn_num')[0],
    che = document.getElementById('che'),
    tel = /^[1][35789]\d{9}$/;
var  del = document.getElementsByClassName('del')[0];

phone.addEventListener('focus', function (event) {
    btn_num.style.bottom = '100px';
    event.preventDefault();
}, false);
che.onclick = function () {
    if(che.checked){
        btn_num.style.bottom = '-403px';
        phone.value = '';
        nextBtn.onclick = null;
        nextBtn.setAttribute('class','nextBtn');
        phone.setAttribute('disabled','disabled');
    }
    if(!che.checked){
        phone.removeAttribute('disabled');
    }

};
    btnNumber.forEach(function (value) {
        var inner = '';
        value.addEventListener('click', function () {
            inner = this.innerHTML;
            phone.value += inner;
            if(phone.value.length === 11 && tel.test(phone.value) && (!che.checked)){
                btn_num.style.bottom = '-402px';
                //            弹出提示框提示
                new AddAlert({
                    hint: '确认输入手机号码',
                    condition:'+(86)' + phone.value,
                    leftText: '确认',
                    rightText: '修改',
                    successFn: function () {
                        btn_num.style.bottom = '-402px';
                        nextBtn.setAttribute('class','on');
//                 发送Ajax请求
                        nextBtn.onclick = function () {
                            ajax({
                                method:'post',
                                url:'http://localhost:8080/register',
                                data:{
                                    telNumber:phone.value,
                                    step:'1'
                                },
                                dataType:'json',
                                contentType:'json',
                                success:function (data) {
                                    if(data.code === 1){
                                        document.cookie = data.msg + data.valid + '';
                                        window.location.href = "./page/verify.html";
                                    }else if(data.code === 2){
                                        // 已有账号
                                        new AddAlert({
                                            hint: '提示',
                                            condition:phone.value + '已经注册,请登录',
                                            leftText: '返回登录',
                                            rightText: '修改',
                                            successFn: function () {
                                                window.location.href = "./login.html";
                                            },
                                            faillFn: function () {
                                                phone.value = '';
                                                btn_num.style.bottom = '100px';
                                            }
                                        });
                                    }
                                }
                                ,
                                fail:function (error) {
                                    console.log(error);
                                }
                            });
                        };
                    },
                    faillFn: function () {
                        btn_num.style.bottom = '100px';
                    }
                });
                nextBtn.addEventListener('click', function () {
                }, false);
            }
            else if(phone.value.length > 11){
                nextBtn.setAttribute('class','nextBtn');
                btn_num.style.bottom = '-402px';
                new AddAlert({
                    hint: '请输入合法手机号码:如下',
                    condition:'(86)+' + 18782777564,
                    leftText: '确认',
                    rightText: '修改',
                    successFn: function () {
                        phone.value = '';
                        btn_num.style.bottom = '100px';
                    },
                    faillFn: function () {
                        phone.value = '';
                        btn_num.style.bottom = '100px';
                    }
                });
            }else {
                nextBtn.setAttribute('class','nextBtn');
            }
            del.onclick = function () {
                var newStr = phone.value.slice(0, phone.value.length-1);
                phone.value = newStr;
            };

        }, false);
    });
